package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpLogMapper;
import com.itheima.pojo.entity.PageResult;
import com.itheima.pojo.log.EmpLog;
import com.itheima.service.EmpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EmpLogServiceImpl implements EmpLogService {

    @Autowired
    private EmpLogMapper empLogMapper;

    /**
     * 保存员工操作日志
     *
     * @param empLog 员工操作日志
     */
    @Override
    @Async
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertLog(EmpLog empLog) {
        empLogMapper.insert(empLog);
    }

    /**
     * 分页查询员工操作日志
     *
     * @param page     页码
     * @param pageSize 每页记录数
     * @return PageResult<EmpLog> 分页查询结果
     */
    @Override
    public PageResult<EmpLog> page(Integer page, Integer pageSize) {
        // 1. 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 2. 执行分页查询
        Page<EmpLog> pageInfo = empLogMapper.page();

        // 3. 返回结果
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }
}
